#!/bin/bash
#############################################
# author:fang
# version : v1.0
# name  : xdir
# dispcripe: do command in dir recusilve
# CopyRight@fangyunjiang[42550564@qq.com]
#############################################

source ~/command/common

# _DEBUG_LEVEL_=2

declare -a _match_paths
declare -a _match_files
_full_root_path=""
_oper=0

function isMatchFile()
{
	local p item
	p="$1/$2"
	for ((i=0; i<${#_match_files[@]}; i++));do
		item="${_match_files[i]}"
		if [ "$item" = "all" -o "${p%%${item}}" != "$p" -o "$2" = "$item" ]; then
			return $_TRUE_
		fi
	done
	return $_FALSE_
}
function isMatchPath()
{
	local p item
	p="$1/$2"
	for ((i=0; i<${#_match_paths[@]}; i++));do
		item="${_match_paths[i]}"
		if [ "$item" = "all" -o "${p%%${item}}" != "$p" -o "$2" = "$item" ]; then
			return $_TRUE_
		fi
	done
	return $_FALSE_
}
function xdirDoRecursiveCommand()
{
	local dir sub_dirs dir_command file_command

	dir=$1; dir_command=$2; file_command=$3
	sub_dirs=($(ls "$dir"))
	for var in "${sub_dirs[@]}";do
		if [ -d "$dir/$var" ];then
			isMatchPath "$dir" "$var" && __warning "match path $dir/$var" && continue
			! [ -z "$dir_command" ] && __exec_target "$dir_command" "$dir/$var" && __debug "complete dir:$dir"
			xdirDoRecursiveCommand "$dir/$var" "$dir_command" "$file_command"
		else
			isMatchFile "$dir" "$var" && __warning "match file $dir/$var" && continue
			! [ -z "$file_command" ] && __exec_target "$file_command" "$dir/$var" && __debug "complete file:$dir/$var"
		fi
	done
}
function xdirCopyFiles()
{
	local dir sub_dirs dest

	dir="$1";dest="$2"
	echo "enter $dir"
	sub_dirs=($(ls "$dir"))
	for var in "${sub_dirs[@]}";do
		if [ -d "$dir/$var" ];then
			isMatchPath "$dir" "$var" && __warning "match path $dir/$var" && continue
			mkdir -p "$dest/$var"
			xdirCopyFiles "$dir/$var" "$dest/$var"
		else
			isMatchFile "$dir" "$var" && __warning "match file $dir/$var" && continue
			cp "$dir/$var" "$dest/$var"
			__debug "copy $var to $dest/$var"
		fi
	done
}
function xdirMoveFiles()
{
	local dir sub_dirs dest

	dir="$1";dest="$2"
	echo "enter $dir"
	sub_dirs=($(ls "$dir"))
	for var in "${sub_dirs[@]}";do
		if [ -d "$dir/$var" ];then
			if isMatchPath "$dir" "$var";then
				mkdir -p "$dest"
				mv "$dir/$var" "$dest/$var"
				__debug "move $dir/$var to $dest/$var"
			else
				xdirMoveFiles "$dir/$var" "$dest/$var"
			fi
		else
			if isMatchFile "$dir" "$var";then
				mkdir -p "$dest"
				mv "$dir/$var" "$dest/$var"
				__debug "move $dir/$var to $dest/$var"
			fi
		fi
	done
}
function copyFilesFromClip() {
	dist_path="$1"
	if __mac ;then
		str=`pbpaste`
	else
		str=`cat /dev/clipboard`
	fi
	str=($str)
	for var in "${str[@]}";do
		path=$(dirname "$var")
		path=${path##*..}
		[ "$path" = "." ]&&path=""
		name=$(basename "${var}")
		mkdir -p "${dist_path}/${path}"
		cp -f "${var}" "${dist_path}/${path}"
		echo "have do copy ${var} to ${dist_path}"
	done
}
function show_help()
{
	local -a list
	list="help"
	list=("${list[@]}" "Usage: xdir [OPTIONS]")
	list=("${list[@]}" "  -r [dir]: set root dir,if not set will be \".\"")
	list=("${list[@]}" "  -d [dir_command]: set command for dir")
	list=("${list[@]}" "  -f [file_command]: set command for file")
	list=("${list[@]}" "  -c [dest_path]: copy files to dest_path(过滤匹配)")
	list=("${list[@]}" "  -m [dest_path]: move files to dest_path(只移动匹配)")
	list=("${list[@]}" "  -e [match_paths:...]: exclude paths(以这些结尾的文件夹[all为所有])")
	list=("${list[@]}" "  -i [match_files:...]: exclude files(以这些结尾的文件[all为所有])")
	list=("${list[@]}" "  -p [dest_path]: 将剪切板中的文件拷贝到dest_path ")
	list=("${list[@]}" "  -h: show help ")
	__msgbox "${list[@]}"
}

function main()
{
	local full_root_path root_path dir_command file_command rp root dest_path
	while getopts :r:d:f:c:e:m:i:p:h opt;do
		case $opt in
			r)root_path="$OPTARG";;
			d)dir_command="$OPTARG";;
			f)file_command="$OPTARG";;
			c)_oper=1;dest_path="$OPTARG";;
			m)_oper=2;dest_path="$OPTARG";;
			e)_match_paths="$OPTARG";;
			i)_match_files="$OPTARG";;
			p)copyFilesFromClip "$OPTARG";exit;;
			*)show_help;exit;;
		esac
	done

	[ -z "$root_path" ]&&root_path="."
	if ! [ -d "$root_path" ];then
		__error "root path is not exist"
		exit
	fi
	_full_root_path=$(__get_full_path "$root_path")

	OLD_IFS=$IFS
	IFS=$':'
	_match_paths=($_match_paths)
	_match_files=($_match_files)
	IFS=$OLD_IFS

	case $_oper in
		0)xdirDoRecursiveCommand "${_full_root_path}" "${dir_command}" "${file_command}";;
		1)mkdir -p "$dest_path"; xdirCopyFiles "${_full_root_path}" "${dest_path}";;
		2)mkdir -p "$dest_path"; xdirMoveFiles "${_full_root_path}" "${dest_path}";;
	esac
}

main "$@"
